package com.ruoyi.web.controller.system.video;

import java.util.Map;

import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.http.HttpUtils;

import io.swagger.annotations.ApiOperation;

/**
 * 视频去水印解析
 * 
 * @author chenjiuchang
 *
 */
@RequestMapping("VideoAnalysis")
@ResponseBody
@RestController
public class VideoAnalysis {
	
	@Value("${ip}")
	private String ip;
	/**
	 * 
	 * @param videoLink 视频链接地址
	 * @return
	 */
	@Anonymous
	@ApiOperation("视屏无水印解析")
	@GetMapping("/watermarklessVideo")
	public AjaxResult watermarklessVideo(String videoLink) {
		String a=ip;
		System.out.println(ip);
		// 获取http第一次出现位置
		int index = videoLink.indexOf("http", 0);
		// 从http开始位置截取
		videoLink = videoLink.substring(index);
		System.out.println(videoLink);

		long start = System.currentTimeMillis();
		String douyinLink = HttpUtils.sendGet("https://tenapi.cn/video/?url=" + videoLink);
//		String douyinLink = HttpUtils.sendGet("https://tenapi.cn/douyin/?url=" + videoLink);
		long end = System.currentTimeMillis();
		System.out.println("接口耗时:" + (end - start) + "ms");
		System.out.println("接口耗时:" + (end - start) / 1000 + "s");
		Map<String, Object> map = new HashedMap<>();
		JSONObject json = JSON.parseObject(douyinLink);
		// VideoDownload
        String txUrl = json.getString("url");
        // 生成视频名称
        String spName = System.currentTimeMillis() + ".mp4";
        // String bdPath = "/Users/tp/shipi/"+spName;
        String bdPath = RuoYiConfig.getProfile()+"/upload/video/"+spName; // 保存到服务器的地址
        System.out.println(bdPath);
        boolean downVideo = VideoDownload.downVideo(txUrl, bdPath);
		map.put("title", json.getString("title"));
		map.put("cover", json.getString("cover"));
		map.put("music", json.getString("music"));
		map.put("url",ip+"/profile/upload/video/"+ spName);
		map.put("ms", (end - start) + "ms");
		return AjaxResult.success(map);
	}

	public static void main(String[] args) {
//		VideoAnalysis videoAnalysis = new VideoAnalysis();
//		String str = "9.48 icn:/ 复制打开抖音，看看【一起看电影吗的作品】“爱是 ______。”— # 今日话题 # 爱情... https://v.douyin.com/rPf1mvn/";
//		AjaxResult AjaxResult = videoAnalysis.watermarklessVideo(str);
//		System.out.println(AjaxResult);
	}

}
